package be.kuleuven.mume.arrayAdapters;

import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.AgendaItem;
import be.kuleuven.mume.framework.Mobilenvi;

public class AgendaArrayAdapter extends ArrayAdapter<AgendaItem>{

	private Context context;
	private List<AgendaItem> listItems;

	public AgendaArrayAdapter(Context context, List<AgendaItem> objects) {
		super(context, R.layout.rij_agenda, objects);
		this.context = context;
		listItems = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.rij_agenda, null);
		}

		AgendaItem u = listItems.get(position);
		if (u == null)
			return view;
		
		setViewText(view, R.id.agendarow_author, getSubject(u));
		setViewText(view, R.id.agendarow_time, getDate(u));
		setViewText(view, R.id.agendarow_location, u.getLocatie());
		
		Mobilenvi.getSingelton().setBackgroundColor(view, u.getTypeItem());
		
		return view;
	}

	private String getSubject(AgendaItem u) {
		String subject;
		String first = "";
		String second = u.getOmschrijving();
		String sepp = "";
		first = u.getVakName();
		if(!u.getOmschrijving().equals("") && !u.getVakName().equals(""))
			sepp=":";
		subject = first + sepp + second;
		return subject;
	}

	private String getDate(AgendaItem u) {
		java.text.DateFormat tf = DateFormat.getTimeFormat(context);
		java.text.DateFormat df = DateFormat.getDateFormat(context);
		
		String date = "";
		if(u.isUpdate())
			date = df.format(u.getDatum()) + " ";
		String start = tf.format(u.getDatum());
		String einde = "";
		if(!(u.getHours() == 0) || !(u.getMinutes() == 0))
			einde = ":" + tf.format(u.getEindDatum());
		
		return date + start + einde;
	}
	
	private void setViewText(View view, int viewId, String text) {
		TextView textv = (TextView) view.findViewById(viewId);
		if (textv != null) {
			textv.setText(text);
		}
	}
}
